package com.ruoyi.buss.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.TimeZone;


public class DateUtilss {
	/**
	 * 取得上上季度最后一天日期
	 * @param date 日期
	 * @return
	 */
	public static String getLastDay(Date date, int jd){
		//取得日历
		Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        
        // 日历减6个月,即上上季度
        calendar.add(Calendar.MONTH,jd);
        
        int year = calendar.get(Calendar.YEAR);// 上上季度的年份
        int mouth = calendar.get(Calendar.MONTH);// 上上季度的月份
        String mmdd = "";// 月/日
        // 根据月份,判断是哪个季度
        switch( mouth){
        	case 1:
        	case 2:	
        	case 3:	
        		mmdd = "0331"; // 第一季度最后一天是3/31
        		break;
        	case 4:
        	case 5:	
        	case 6:	
        		mmdd = "0630"; // 第二季度最后一天是6/30
        		break;
        	case 7:
        	case 8:	
        	case 9:	
        		mmdd = "0930";// 第三季度最后一天是9/30
        		break;
        	case 10:
        	case 11:	
        	case 12:	
        		mmdd = "1231";// 第四季度最后一天是12/31
        		break;
        }
        
        String lastDate = String.valueOf(year)   + mmdd;
        return lastDate;
	}
	
/**
 * 获取指定范围内每年的每个月最后一天
 * @param year 多少年范围内(例如 3 就是三年内,1就是本年(一年)内)
 * @param zdYear 指定年(若这个参数不为0,则yaer参数自动失效)
 * @return
 */
	public static ArrayList<String> getEndMonthByYear(int year,int zdYear) {
		
		Integer[] list = new Integer[12];
		for (int i = 0; i < 12; i++) {
			list[i] = i + 1;
		}
		
		if(zdYear != 0) {
			year= 1;
		}
		ArrayList<String> sjrqs = new ArrayList<String>();
		for (int y = 0; y < year; y++) {
			for (int i : list) {
				Calendar cal = Calendar.getInstance();
				int years = Integer.valueOf(new SimpleDateFormat("yyyy").format(new Date())) - y;
				if(zdYear!=0) {
					years= zdYear;
				}
				// 设置年份
				cal.set(Calendar.YEAR, years);
				// 设置月份
				cal.set(Calendar.MONTH, i - 1);
				// 获取某月最大天数
				int lastDay = cal.getActualMaximum(Calendar.DATE);
				// 设置日历中月份的最大天数
				cal.set(Calendar.DAY_OF_MONTH, lastDay);
				// 格式化日期
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
				int a = Integer.valueOf(sdf.format(cal.getTime()));
				//年份修改为2019    月份改为当前月   日改为每个月最大的日期
				LocalDate localDate = LocalDate.of(a, 1, 1).withYear(a).withDayOfMonth(cal.getActualMaximum(Calendar.DATE)).with(ChronoField.MONTH_OF_YEAR, i);
				String sjrq = String.valueOf(localDate).replace("-", "");
				sjrqs.add(sjrq);
			}
		}
		String yesterday = new SimpleDateFormat("yyyyMMdd").format(new Date(new Date().getTime() - 2*24*60*60*1000));
		sjrqs.add(yesterday);
		return sjrqs;

	}
	
	public static ArrayList<String> getSunDayByYear() {
		
		
		ArrayList<String> sjrqs = new ArrayList<String>();
		ArrayList<String> sjrq = new ArrayList<String>();
		String year = new SimpleDateFormat("yyyy").format(new Date());
        Calendar c = Calendar.getInstance();
        c.set(Integer.parseInt(year), 0, 1);
        Calendar c2 = Calendar.getInstance();
        c2.set(Integer.parseInt(year) + 1, 0, 1);
        
        while (c.compareTo(c2) < 0) {
        	String M = "";
            String d= "";
            if((c.get(Calendar.MONTH) + 1)<10) 
            	M = "0";
            if(c.get(Calendar.DAY_OF_MONTH)<10)
            	d = "0";
            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                sjrqs.add(c.get(Calendar.YEAR) +M+(c.get(Calendar.MONTH) + 1) + d+ + c.get(Calendar.DAY_OF_MONTH));
            }
            // 日期+1
            c.add(Calendar.DATE, 1);
        }
        String yesterday = new SimpleDateFormat("yyyyMMdd").format(new Date(new Date().getTime() - 24*60*60*1000));

		for(String str :sjrqs) {
			if(Integer.valueOf(str)<=Integer.valueOf(yesterday)) {
				sjrq.add(str);
			}
		}
		return sjrq;

	}

	/**
	 * 获取前一天的日期
	 * @return
	 */
	public static Date getdate(int i) {
		Date date = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, i);
		date = calendar.getTime();
		return date;
	}
	/**
	 * 	取得年末的日期
	 * @param date 日期
	 * @param jd -1 去年 -2前年  一次类推
	 * @return
	 */
	public static String getLastYear(Date date, int jd){
		//取得日历
		Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        
        // 日历减6个月,即上上季度
        calendar.add(Calendar.YEAR,jd);
        
        int year = calendar.get(Calendar.YEAR);// 上上季度的年份
        String mmdd = "1231";// 月/日
        
        String lastDate = String.valueOf(year)  + mmdd;
        return lastDate;
	}

	/**
	 * 获得该月最后一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	 public static String getLastDayOfMonth(int month) {
	    Calendar cal = Calendar.getInstance();
	    cal.getActualMaximum(Calendar.DATE);
	    
	    
	    // 设置月份
	    cal.set(Calendar.MONTH, month-1);
	    // 获取某月最大天数
	    int lastDay=0;
	   //2月的平年瑞年天数
	   if(month==2) {
	     lastDay = cal.getLeastMaximum(Calendar.DAY_OF_MONTH);
	   }else {
	     lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	   }
	    // 设置日历中月份的最大天数
	     cal.set(Calendar.DAY_OF_MONTH, lastDay);
	    // 格式化日期
	    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
	    String lastDayOfMonth = sdf.format(cal.getTime());
	    return lastDayOfMonth;
	 }
	 public static String getLastDayOfMonths(int month) {
		    Calendar cal = Calendar.getInstance();
		    cal.getActualMaximum(Calendar.DATE);
		    
		    
		    // 设置月份
		    cal.set(Calendar.MONTH, month-1);
		    // 获取某月最大天数
		    int lastDay=0;
		   //2月的平年瑞年天数
		   if(month==2) {
		     lastDay = cal.getLeastMaximum(Calendar.DAY_OF_MONTH);
		   }else {
		     lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		   }
		    // 设置日历中月份的最大天数
		     cal.set(Calendar.DAY_OF_MONTH, lastDay);
		    // 格式化日期
		    SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
		    String lastDayOfMonth = sdf.format(cal.getTime());
		    return lastDayOfMonth;
		 }
	 /**
	  * 	获取近3年4个季度及当前每个月的月份
	  * @param month
	  * @return
	  */
	 public static ArrayList<String> getThreeYearFourQuarterAndMonth() {
		HashSet<String> hashSet = new HashSet<String>(); 
		 
		String twoYearAgo = DateUtilss.getLastYear(new Date(),-2);//俩年前
		String ontYearAgo = DateUtilss.getLastYear(new Date(),-1);//一年前
		
		String fjd  =  DateUtilss.getLastDay(new Date(),-3); //上个季度
		String tjd  =  DateUtilss.getLastDay(new Date(),-6); //上2个季度 
		String sjd  =  DateUtilss.getLastDay(new Date(),-9); //上3个季度 
		String ojd  =  DateUtilss.getLastDay(new Date(),-12); //上3个季度 
		
		hashSet.add(twoYearAgo);
		hashSet.add(ontYearAgo);
		hashSet.add(fjd);
		hashSet.add(tjd);
		hashSet.add(sjd);
		hashSet.add(ojd);
		
		Integer time[] = {1,2,3,4,5,6,7,8,9,10,11,12}; 
	    for (Integer integer : time) {
			 hashSet.add(DateUtilss.getLastDayOfMonth(integer));
		}
	    
	    ArrayList<String> result = new ArrayList<>(hashSet);
		Collections.sort(result);

		return result;
	 }
	public static Date beginOfDate(Date date) {
		Date newDate = new Date();
	    long current = date.getTime()+1000*3600*24;
	    long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
        try {  
        	newDate = new Timestamp(zero);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
		return newDate;
	}
	public static Date endOfDate(Date date) {
		Date newDate = new Date();
	    long current = date.getTime()+1000*3600*24;
	    long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
        long twelve=zero+24*60*60*1000-1;//今天23点59分59秒的毫秒数
        try {  
        	newDate = new Timestamp(twelve);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
		return newDate;
	}
	
	public static Date strToDate(String datestr) {
		datestr = datestr.replace("-", "");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date strToDate = new Date();
		try {
			strToDate = sdf.parse(datestr);
			System.out.println(strToDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return strToDate;
	}
}
